/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************
 * MIFSS  - content storage system
 * 
 *
 * @uthors: uros.kristan@gmail.com (Uroš Kristan ) Urosk.NET
 *         jernej.svigelj@gmail.com (Jernej Švigelj) 
 */
package net.monare.mifss.workers;

import net.monare.mifss.configurations.pojo.ApiKey;
import net.monare.mifss.configurations.pojo.ApiMode;
import net.monare.mifss.exceptions.ApiKeyHandlerException;
import net.monare.mifss.workers.interfaces.ApiKeyHandler;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Set;
import java.util.UUID;

@Component
public class ApiKeyHandlerImpl implements ApiKeyHandler {

    @Override
    public ApiKey verifyApiKey(Set<ApiKey> availableKeys, String apiKeyHashToBeVefified, ApiMode apiMode) throws ApiKeyHandlerException {

        ApiKey apiKey = null;
        for (ApiKey ak : availableKeys) {
            if (apiKeyHashToBeVefified.equals(ak.getHash()))
                apiKey = ak;
            break;
        }

        if (apiKey == null || apiMode != apiKey.getApiMode()) {

            throw new ApiKeyHandlerException("No valid api key found for given hashKey!");
        }

        return null;

    }

    @Override
    public ApiKey generateNewApiKey(String name, String description, ApiMode apiMode) throws ApiKeyHandlerException {

        // api key name is mandatory
        if (StringUtils.isEmpty(name)) {
            throw new ApiKeyHandlerException("Name must be entered");
        }
        // api mode must be explicitly entered
        if (apiMode == null) {
            throw new ApiKeyHandlerException("Api mode must be entered! Possible values are: " + ApiMode.values());
        }

        String hash = DigestUtils.shaHex("secret x" + (new Date().getTime()) + "mifss" + UUID.randomUUID() + Math.random());

        ApiKey apiKey = new ApiKey();

        apiKey.setApiMode(apiMode);
        apiKey.setDescription(description);
        apiKey.setHash(hash);
        apiKey.setName(name);

        return apiKey;
    }

}
